package com.tank.elk.elasticsearch;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: SuQiEr
 * Date:2017/10/26
 * Time: 14:13
 * To change this template use File | Settings | File Templates.
 */
public class StringTest {

    public static void main(String[] args) {

        String str = "[2017-10-26 09:48:52,528] [DEBUG] c.b.a.d.m.PayOrderDOMapper.selectByPage 142 -- [6a95e869-5092-45b9-8cba-f4081e97ce47] ==> Preparing: SELECT ID, MERCHANT_NO, SUB_MERCHANT_NO, REQUEST_TIME, MERCHANT_ORDER_ID, AMOUNT, TITLE, DETAIL, PAY_TYPE, STORE_NO, TIME_EXPRESS, OPERATOR_NO, QR_CODE, PRODUCT_TYPE, SUB_PRODUCT_TYPE, TRADE_NO, SUCCESS_TIME, CREATED_AT, UPDATED_AT, CREATED_BY, UPDATED_BY, STATUS, SIGN_TYPE, FORMAT, MEMO, VERSION, METHOD, ERROR_CODE, ERROR_MSG, TRANS_NO, SERVICE_URL, CHANNEL_ORDER_NO, PAY_ORDER_NO FROM T_PAY_ORDER WHERE";
//        Pattern p=Pattern.compile("[\\d]");
//        Matcher m=p.matcher(str);
//        System.out.println(m.group());

        Pattern p=Pattern.compile("\\[([^\\[\\]]+)\\]");
        Matcher m=p.matcher(str);
        int index = 0;
        String traceLogId = "";
        while (m.find()){
            if(index == 2){
                traceLogId = m.group(1);
            }
            index++;
        }
        System.out.println(traceLogId);

//        Matcher m=p.matcher("[f4081e97ce47]");
//        System.out.println(m.find()); //匹配aaa2223
//        System.out.println(m.group()); //返回2,因为有2组

//        System.out.println(m.start(1)); //返回0 返回第一组匹配到的子字符串在字符串中的索引号
//        System.out.println(m.start(2)); //返回3
//        System.out.println(m.end(1)); //返回3 返回第一组匹配到的子字符串的最后一个字符在字符串中的索引位置.
//        System.out.println(m.end(2)); //返回7
//        System.out.println(m.group(1)); //返回aaa,返回第一组匹配到的子字符串
//        System.out.println(m.group(2)); //返回2223,返回第二组匹配到的子字符串
    }

}
